/**
 * Title:        流程图特殊字段检测
 * Company:      泛微软件
 */


package checkDetail;

import general.BaseBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import weaver.conn.RecordSet;
import weaver.general.Util;



public class FlowChartSpecialCharCheck extends BaseBean implements CheckBase  {

    public static String savedids;
    @Override
    public JSONObject checkData(HashMap dataMap) {
        setRecordSet();
        (new BaseBean()).writeLog("SameFieldNameCheck  start");
        List resultList = new ArrayList();
        String result = resultOk;
        String sqlStr = "";
        sqlStr +=" select id,nodename from workflow_nodebase where nodename like '%/%' or nodename like '%<%' or nodename like '%>%' or nodename like '%''%'  or nodename like '%\"%' ";
        recordSet.executeSql(sqlStr);
        int cnt = 0;
        String nodeids = "";
        while(recordSet.next()){
            result = resultNeedFix;
            Map resultMap = new HashMap<String,String>();
            resultMap.put("showMessage", "节点[" + recordSet.getString("nodename") + "]节点名称存在特殊字符，需要修复。");
            resultMap.put("nodeid",recordSet.getString("id"));
            resultMap.put("buttonname","去掉特殊字符");
            resultList.add(resultMap);
        }
        (new BaseBean()).writeLog("SameFieldNameCheck  end");
        return formatReturnJsonCheck(result, resultList, this);
    }

    @Override
    public JSONObject fixData(HashMap dataMap) {
        setRecordSet();
        List resultList = new ArrayList();
        String result = "";
        if(dataMap!= null && dataMap.get("divid")!= null){
            String divid = (String)dataMap.get("divid");
            String nodeid = (String)dataMap.get("nodeid");

            String sqlStr = "";
            sqlStr =" update workflow_nodebase set nodename = replace(replace(replace(replace(replace(nodename,'/',''),'<',''),'>',''),'''',''),'\"','') where id = " + nodeid;
            recordSet.executeUpdate(sqlStr);

            sqlStr +=" select id,nodename from workflow_nodebase where id = " + nodeid + " and nodename like '%/%' or nodename like '%<%' or nodename like '%>%' or nodename like '%''%'  or nodename like '%\"%' ";
            recordSet.executeSql(sqlStr);
            if(recordSet.next()){
                result = resultNeedFix;
                Map resultMap = new HashMap<String,String>();
                resultMap.put("result", resultNeedFix);
                resultMap.put("fixMessage", "修复失败");
                resultMap.put("divid",divid);
                resultList.add(resultMap);
            }else{
                result = resultOk;
                Map resultMap = new HashMap<String,String>();
                resultMap.put("result", resultOk);
                resultMap.put("fixMessage", "修复成功");
                resultMap.put("divid",divid);
                resultList.add(resultMap);
            }
            return formatReturnJsonCheck(result, resultList, this);
        }else{
            String sqlStr = "";
            sqlStr +=" update workflow_nodebase set nodename = replace(replace(replace(replace(replace(nodename,'/',''),'<',''),'>',''),'''',''),'\"','') " ;
            sqlStr +=" where nodename like '%/%' or nodename like '%<%' or nodename like '%>%' or nodename like '%''%'  or nodename like '%\"%' ";
            recordSet.executeUpdate(sqlStr);
            return checkData(new HashMap());
        }
    }

    @Override
    public String getClassName() {
        return "流程图特殊字段检测";
    }

    @Override
    public String getClassDesc() {
        return "检测流程名字和节点名字是否有特殊字符，会导致流程图无法显示的问题";
    }
}
